const HtmlWebpackPlugin = require('html-webpack-plugin');
// 请求打包的静态文件
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
// 可视化资源分析
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
// 分离CSS和JS文件
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const webpack = require('webpack');

exports.getPlugins = (conf) => {
  let plugins = [
    // new HtmlWebpackPlugin({
    //   template: `${ROOT_PATH}/app/index.html`,
    //   filename: "./index.html",
    //   minify: {
    //     removeComments: true,
    //     collapseWhitespace: true,
    //     removeRedundantAttributes: true,
    //     useShortDoctype: true,
    //     removeEmptyAttributes: true,
    //     removeStyleLinkTypeAttributes: true,
    //     keepClosingSlash: true,
    //     minifyJS: true,
    //     minifyCSS: true,
    //     minifyURLs: true,
    //   },
    //   title: 'index',
    //   chunks: ['index'],
    // }),
  ];

  if (conf.env === "development") {
    // 热更新插件 通过api启用inline模式
    plugins.push(new webpack.HotModuleReplacementPlugin());
    
  } else {
    // 清空上一次打包的文件
    plugins.push(new CleanWebpackPlugin());
  }
  // 可视化资源分析插件
  plugins.push(new BundleAnalyzerPlugin());
  // 分离CSS和JS文件
  plugins.push(new MiniCssExtractPlugin({
    filename: 'css/[name].css',
    chunkFilename: 'css/[name].css',
    allChunks: true,
  }));

  return plugins;
}